{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/13_zonal_statistics_by_group.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Analyzing National Land Cover Database (NLCD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "### Add NLCD data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n",
    "landcover = ee.Image(dataset.select(\"landcover\"))\n",
    "Map.addLayer(landcover, {}, \"NLCD 2016\")\n",
    "\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "Map.addLayer(states, {}, \"US States\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_legend(builtin_legend=\"NLCD\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "### Calculate land cover compostion of each US state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "nlcd_stats = os.path.join(out_dir, \"nlcd_stats.csv\")\n",
    "\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)\n",
    "\n",
    "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n",
    "# denominator can be used to convert square meters to other areal units, such as square kilometers\n",
    "geemap.zonal_stats_by_group(\n",
    "    landcover,\n",
    "    states,\n",
    "    nlcd_stats,\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1000000,\n",
    "    decimal_places=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.create_download_link(nlcd_stats)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "## Analyzing Global Land Cover"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "### Add MODIS global land cover data\n",
    "\n",
    "MODIS MCD12Q1.006 Land Cover Type Yearly Global 500m \n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "landcover = ee.Image(\"MODIS/006/MCD12Q1/2013_01_01\").select(\"LC_Type1\")\n",
    "\n",
    "vis = {\n",
    "    \"min\": 1.0,\n",
    "    \"max\": 17.0,\n",
    "    \"palette\": [\n",
    "        \"05450a\",\n",
    "        \"086a10\",\n",
    "        \"54a708\",\n",
    "        \"78d203\",\n",
    "        \"009900\",\n",
    "        \"c6b044\",\n",
    "        \"dcd159\",\n",
    "        \"dade48\",\n",
    "        \"fbff13\",\n",
    "        \"b6ff05\",\n",
    "        \"27ff87\",\n",
    "        \"c24f44\",\n",
    "        \"a5a5a5\",\n",
    "        \"ff6d4c\",\n",
    "        \"69fff8\",\n",
    "        \"f9ffa4\",\n",
    "        \"1c0dff\",\n",
    "    ],\n",
    "}\n",
    "\n",
    "Map.setCenter(6.746, 46.529, 2)\n",
    "Map.addLayer(landcover, vis, \"MODIS Land Cover\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_legend(builtin_legend=\"MODIS/006/MCD12Q1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "countries_shp = \"../data/countries.shp\"\n",
    "countries = geemap.shp_to_ee(countries_shp)\n",
    "Map.addLayer(countries, {}, \"Countries\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "global_stats = os.path.join(out_dir, \"global_stats.csv\")\n",
    "\n",
    "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n",
    "# denominator can be used to convert square meters to other areal units, such as square kilimeters\n",
    "geemap.zonal_stats_by_group(\n",
    "    landcover,\n",
    "    countries,\n",
    "    global_stats,\n",
    "    stat_type=\"PERCENTAGE\",\n",
    "    denominator=1000000,\n",
    "    decimal_places=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.create_download_link(global_stats)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
